package units;


//position @ Array
//Archer // Short // Long // 2-Hand // Spear // Hallberdier // Hoplit // Ballista // Siege - Ram // Catapult // Trebuchet // Tower

import java.util.Random;
import game.Costs;

public abstract class AbstractUnit {

	private int healthpoints = 0;
	private int attack = 0;
	private int attackMultiple = 0;
	private int defense = 0;
	private int attackquality = 0; //from 0 to 100 in %
	private int[] attackRounds = {};
	private int buildCountdown = 0;	
	private Costs costs = null;
	
	abstract int getAttackBonus();
	abstract int getDefenseBonus();
	abstract int getAttackqualityBonus();
	abstract int getHealthpointsBonus();
	
	public int getHealthpoints() {
		return healthpoints;
	}
	public void setHealthpoints(int healthpoints) {
		this.healthpoints = healthpoints;
	}
	public int getAttack() {
		return attack;
	}
	public void setAttack(int attack) {
		this.attack = attack;
	}
	
	public int getAttackMultiple() {
		return attackMultiple;
	}
	public void setAttackMultiple(int attackMultiple) {
		this.attackMultiple = attackMultiple;
	}
	public int getDefense() {
		return defense;
	}
	public void setDefense(int defense) {
		this.defense = defense;
	}
	public int getAttackquality() {
		return attackquality;
	}
	public void setAttackquality(int attackquality) {
		this.attackquality = attackquality;
	}
	public int[] getAttackRounds() {
		return attackRounds;
	}
	public void setAttackRounds(int[] attackRounds) {
		this.attackRounds = attackRounds;
	}

	public Costs getCosts() {
		return costs;
	}
	public void setCosts(Costs costs) {
		this.costs = costs;
	}
	
	public boolean isBuilded() {
		if (this.getBuildCountdown()==0) {
			return true;
		} else {
			return false;
		}
	}
	
	
	
	//-----------
	
	AbstractUnit(int healthpoints, int attack, int defense, int attackquality, int attackMultiple, int[] attackRounds, int buildCountdown) {
		this.healthpoints = healthpoints + this.getHealthpointsBonus();
		this.attack = attack;
		this.defense = defense;
		this.attackquality = attackquality;
		this.attackMultiple = attackMultiple;
		this.attackRounds = attackRounds;
		this.buildCountdown = buildCountdown;
	}
	
	int hurt(int damage) {
		if (healthpoints < damage) {
			int lostHealthpoints = healthpoints;
			healthpoints = 0;
			return lostHealthpoints;
		}
		healthpoints = healthpoints - damage;
		return damage;
	}
	
	int attack(AbstractUnit enemy) {
		int damage = (this.attack + this.getAttackBonus()) - (enemy.defense + enemy.getDefenseBonus());
		if (damage < 0) {damage = 0;}
		
		if (damage == 0){
			//Verteidigung > Angriff ---> Kein Schaden
			return 0;
		} else {
			Random ra = new Random();
			int testAttackquality;
			testAttackquality = ra.nextInt(100) +1;
			if (testAttackquality <= (this.attackquality + this.getAttackqualityBonus())) {
				//Probe auf Angriffswahrscheinlichkeit gelingt -> Angriff
				enemy.hurt(damage);
				return damage;
			} else {
				//Probe auf Angriffswahrscheinlichkeit missling -> Geblockter Angriffsversuch/Daneben
				return 0;
			}
		}
	}
	
	public int getBuildCountdown() {
		return buildCountdown;
	}
	public void setBuildCountdown(int buildCountdown) {
		this.buildCountdown = buildCountdown;
	}
	
	boolean isAlive() {
		if (healthpoints > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	boolean canAttack(int round) {

		for (int i=0; i<attackRounds.length;i++) {
			//System.out.print(attackRounds[i]);
			if (attackRounds[i] == round) {
				return true;
			}
		}
		return false;
	}
	public String getName() {
		return this.getClass().getSimpleName();
	}

}
